
cmake_minimum_required(VERSION 3.20.0)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)

add_subdirectory(src/apps/ColorImage ColorImage)

if (EMSCRIPTEN)
    set(3DPARTY_INSTALL_DIRECTORY "/mnt/d/workspace/3dparty/prebuilt/Emscripten/Release")
    add_library(zlib_interface INTERFACE)
    target_include_directories(zlib_interface INTERFACE ${3DPARTY_INSTALL_DIRECTORY}/zlib/include)
    target_link_libraries(zlib_interface INTERFACE ${3DPARTY_INSTALL_DIRECTORY}/zlib/lib/libz.a)
    target_compile_definitions(zlib_interface INTERFACE USE_ZLIB)

    add_library(libzip_interface INTERFACE)
    target_include_directories(libzip_interface INTERFACE ${3DPARTY_INSTALL_DIRECTORY}/libzip/include)
    target_link_libraries(libzip_interface INTERFACE ${3DPARTY_INSTALL_DIRECTORY}/libzip/lib/libzip.a)
    target_compile_definitions(libzip_interface INTERFACE USE_LIBZIP)
else()
    if("${CMAKE_BUILD_TYPE}" STREQUAL "Release")
        set(3DPARTY_INSTALL_DIRECTORY "D:/workspace/3dparty/prebuilt/Windows/Release")
        add_library(zlib_interface INTERFACE)
        target_include_directories(zlib_interface INTERFACE ${3DPARTY_INSTALL_DIRECTORY}/zlib/include)
        target_link_libraries(zlib_interface INTERFACE ${3DPARTY_INSTALL_DIRECTORY}/zlib/lib/zlibstatic.lib)
        target_compile_definitions(zlib_interface INTERFACE USE_ZLIB)

        add_library(libzip_interface INTERFACE)
        target_include_directories(libzip_interface INTERFACE ${3DPARTY_INSTALL_DIRECTORY}/libzip/include)
        target_link_libraries(libzip_interface INTERFACE ${3DPARTY_INSTALL_DIRECTORY}/libzip/lib/zip.lib)
        target_compile_definitions(libzip_interface INTERFACE USE_LIBZIP)
    else()
        set(3DPARTY_INSTALL_DIRECTORY "D:/workspace/3dparty/prebuilt/Windows/Debug")
        add_library(zlib_interface INTERFACE)
        target_include_directories(zlib_interface INTERFACE ${3DPARTY_INSTALL_DIRECTORY}/zlib/include)
        target_link_libraries(zlib_interface INTERFACE ${3DPARTY_INSTALL_DIRECTORY}/zlib/lib/zlibstaticd.lib)
        target_compile_definitions(zlib_interface INTERFACE USE_ZLIB)

        add_library(libzip_interface INTERFACE)
        target_include_directories(libzip_interface INTERFACE ${3DPARTY_INSTALL_DIRECTORY}/libzip/include)
        target_link_libraries(libzip_interface INTERFACE ${3DPARTY_INSTALL_DIRECTORY}/libzip/lib/zip.lib)
        target_compile_definitions(libzip_interface INTERFACE USE_LIBZIP)
    endif()
endif()

project(darknet)

aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/src Source_File_List)
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/src/cores Source_Cores_File_List)
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/src/utils Source_Utils_File_List)
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/src/layers Source_Layers_File_List)
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/src/models Source_Models_File_List)

list(APPEND Source_File_List ${Source_Cores_File_List})
list(APPEND Source_File_List ${Source_Utils_File_List})
list(APPEND Source_File_List ${Source_Layers_File_List})
list(APPEND Source_File_List ${Source_Models_File_List})

if(EMSCRIPTEN)
    aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/src/apps Source_Apps_File_List)
    set(EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/web)

    # set(EXECUTABLE_OUTPUT_PATH "D:/workspace/npl/webparacraft/camera")
    # add_link_options(-g3 -gsource-map -lidbfs.js -lembind -sFORCE_FILESYSTEM -sASYNCIFY -sFETCH -sALLOW_MEMORY_GROWTH -sENVIRONMENT=web)
    add_link_options(-O3 -lidbfs.js -lembind -sFORCE_FILESYSTEM -sASYNCIFY -sFETCH -sALLOW_MEMORY_GROWTH -sENVIRONMENT=web)
    add_executable(${PROJECT_NAME} ${Source_File_List} ${Source_Apps_File_List} main.cpp esp.cpp)
    target_include_directories(${PROJECT_NAME} PUBLIC include src src/apps)
    target_link_libraries(${PROJECT_NAME} PUBLIC libzip_interface zlib_interface)

    # add_custom_target(copy_model ALL
    #     COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/data/colors/models/colors.model.lastest ${CMAKE_CURRENT_SOURCE_DIR}/web/asset/models/colors.model.lastest
    #     COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/data/roadsigns/models/roadsigns.model.lastest ${CMAKE_CURRENT_SOURCE_DIR}/web/asset/models/roadsigns.model.lastest
    #     COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/data/roadlines/models/roadlines.model.lastest ${CMAKE_CURRENT_SOURCE_DIR}/web/asset/models/roadlines.model.lastest
    #     COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/data/goods/models/goods.model.lastest ${CMAKE_CURRENT_SOURCE_DIR}/web/asset/models/goods.model.lastest
    #     COMMENT "拷贝模型文件"
    # )

    # 拷贝文件系统到根目录
    add_custom_target(
        asset ALL
        COMMAND $ENV{EMSDK}/upstream/emscripten/tools/file_packager asset.data --preload "${CMAKE_CURRENT_SOURCE_DIR}/web/asset@/" --js-output=asset.js
        COMMENT "打包资源文件"
        OUTPUT_DIRECTORY ${EXECUTABLE_OUTPUT_PATH}
        WORKING_DIRECTORY ${EXECUTABLE_OUTPUT_PATH}
    )
else()
    aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/src/apps Source_Apps_File_List)
    add_executable(${PROJECT_NAME} ${Source_File_List} ${Source_Apps_File_List} main.cpp esp.cpp)
    target_include_directories(${PROJECT_NAME} PUBLIC include src src/apps)
    target_link_libraries(${PROJECT_NAME} PUBLIC libzip_interface zlib_interface)

    add_executable(LabelMeConvertDarknet LabelMeConvertDarknet.cpp)
    target_include_directories(LabelMeConvertDarknet PUBLIC include)

    # add_subdirectory(mylibrary)
    set(LUA_DIR "D:/workspace/npl/NPLRuntime_win32/Server/trunk/LuaJIT")
    add_library(LuaDarkNet SHARED LuaDarkNet.cpp ${Source_File_List} ${Source_Utils_File_List} ${Source_Layers_File_List})
    target_link_libraries(LuaDarkNet PUBLIC "D:/workspace/npl/NPLRuntime_win32/build/lib/Release/lua.lib")
    target_include_directories(LuaDarkNet PUBLIC "${LUA_DIR}/src" "${CMAKE_CURRENT_SOURCE_DIR}/include" "${CMAKE_CURRENT_SOURCE_DIR}/src" "${CMAKE_CURRENT_SOURCE_DIR}/src/utils" "${CMAKE_CURRENT_SOURCE_DIR}/src/layers" "D:/workspace/npl/NPLRuntime_win32/build/LuaJIT")
endif()

# emcmake -S . -B build/emscripten -DEMSCRIPTEN=TRUE